package com.example.redcultural;


import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class tour extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    int s = 0, e = 7;
    private int old = 0;
    private String mParam1;
    private String mParam2;
    private List<HashMap<String, Object>> mData;  //M
    private ListView listView;  //V
    private TextView ss, hsgjd, qt;
    private OnFragmentInteractionListener mListener;
    private FloatingActionButton floatingActionButton;


    public static tour newInstance(String param1, String param2) {
        tour fragment = new tour();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View decorView = inflater.inflate(R.layout.fragment_tour, container, false);
        ss = decorView.findViewById(R.id.tour_ss);
        hsgjd = decorView.findViewById(R.id.tour_hsgjd);
        qt = decorView.findViewById(R.id.tour_qt);
        listView = decorView.findViewById(R.id.listView);
        floatingActionButton = decorView.findViewById(R.id.bjfl);


        List<String> images = new ArrayList<>();
        images.add("http://www.jgscct.com/Plugin/ueditor/net/upload/2015-09-21/8819b8e0-e737-44b1-8921-1b0a6114b55e.jpg");
        images.add("http://staticfile.tujia.com/upload/info/day_131206/201312060533432761.jpg");
        images.add("https://img.alicdn.com/bao/uploaded/i3/TB1HE.kAHvpK1RjSZPiEO_mwXXa_065403.jpg_400x400.jpg");
        images.add("http://www.jxsms.cn/upload/201809/1538018178.jpg");
        //设置轮播
        Banner banner = decorView.findViewById(R.id.tour_banner);
        //轮播监听
        banner.setImages(images).setImageLoader(new GlideImageLoader()).setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(getContext(), "敬请期待" + position, Toast.LENGTH_SHORT).show();
            }
        }).start();
        final String[] money = getResources().getStringArray(R.array.money);
        final String[] info = getResources().getStringArray(R.array.info);
        final String[] imageIds = getResources().getStringArray(R.array.imageUrl);
        final String[] title = getResources().getStringArray(R.array.tour_title);
        final List<TourContent> tourContents = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            TourContent tourContent = new TourContent();
            tourContent.setMoney(money[i]);
            tourContent.setInfo(info[i]);
            tourContent.setId(imageIds[i]);
            tourContent.setTitle(title[i]);
            tourContents.add(tourContent);
        }
        listView.setAdapter(new MyAdapter(getContext(), tourContents));
        ss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                s = 0;
                e = 7;
                List<TourContent> tourContents = new ArrayList<>();
                for (int i = s; i < e; i++) {
                    TourContent tourContent = new TourContent();
                    tourContent.setMoney(money[i]);
                    tourContent.setInfo(info[i]);
                    tourContent.setId(imageIds[i]);
                    tourContent.setTitle(title[i]);
                    tourContents.add(tourContent);
                }
                ss.setBackgroundResource(R.drawable.shape_bj);
                hsgjd.setBackgroundColor(Color.WHITE);
                qt.setBackgroundColor(Color.WHITE);
                listView.setAdapter(new MyAdapter(getContext(), tourContents));
            }
        });
        hsgjd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                s = 7;
                e = 14;
                List<TourContent> tourContents = new ArrayList<>();
                for (int i = s; i < e; i++) {
                    TourContent tourContent = new TourContent();
                    tourContent.setMoney(money[i]);
                    tourContent.setInfo(info[i]);
                    tourContent.setId(imageIds[i]);
                    tourContent.setTitle(title[i]);
                    tourContents.add(tourContent);
                }
                ss.setBackgroundColor(Color.WHITE);
                hsgjd.setBackgroundResource(R.drawable.shape_bj);
                qt.setBackgroundColor(Color.WHITE);
                listView.setAdapter(new MyAdapter(getContext(), tourContents));
            }
        });
        qt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                s = 14;
                e = 19;
                List<TourContent> tourContents = new ArrayList<>();
                for (int i = s; i < e; i++) {
                    TourContent tourContent = new TourContent();
                    tourContent.setMoney(money[i]);
                    tourContent.setInfo(info[i]);
                    tourContent.setId(imageIds[i]);
                    tourContent.setTitle(title[i]);
                    tourContents.add(tourContent);
                }
                ss.setBackgroundColor(Color.WHITE);
                hsgjd.setBackgroundColor(Color.WHITE);
                qt.setBackgroundResource(R.drawable.shape_bj);
                listView.setAdapter(new MyAdapter(getContext(), tourContents));
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                TextView info=view.findViewById(R.id.in);
                TextView title=view.findViewById(R.id.tour_title);
                new AlertDialog.Builder(getActivity())
                        .setTitle("景点："+title.getText())
                        .setMessage(""+info.getText())
                        .setPositiveButton("确定", null)
                        .show();
            }
        });

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 AlertDialog alertDialog= new AlertDialog.Builder(getContext())
                        .setView(R.layout.tour_pic)
                         .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                             @Override
                             public void onClick(DialogInterface alertDialog, int which) {
                                 alertDialog.dismiss();
                             }
                         })
                        .create();

                alertDialog.show();


                //调用该方法隐藏对话框
//                alertDialog.dismiss();
            }
        });


        /*listView.setOnScrollListener(new AbsListView.OnScrollListener() {

            View topLine = decorView.findViewById(R.id.topLine);

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                if (firstVisibleItem > old) {


                    //平移动画
                    TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                            Animation.RELATIVE_TO_SELF, 0.0f,
                            Animation.RELATIVE_TO_SELF, 0.0f,
                            Animation.RELATIVE_TO_SELF, -1.0f);
                    translateAnimation.setFillAfter(true);
                    translateAnimation.setDuration(250);
                    translateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());

                    //渐变动画
                    AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
                    alphaAnimation.setFillAfter(true);
                    alphaAnimation.setDuration(250);

                    AnimationSet animationSet = new AnimationSet(true);
                    animationSet.addAnimation(translateAnimation);
                    animationSet.addAnimation(alphaAnimation);


                    //设置顶部视图不可见，并加载动画
                    topLine.startAnimation(animationSet);
                    topLine.setVisibility(View.GONE);

                    ((Activity) getContext()).getWindow().setStatusBarColor(Color.WHITE);


                } else if (firstVisibleItem < old) {


                    //平移动画
                    TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                            Animation.RELATIVE_TO_SELF, 0.0f,
                            Animation.RELATIVE_TO_SELF, -1.0f,
                            Animation.RELATIVE_TO_SELF, 0.0f);
                    translateAnimation.setFillAfter(true);
                    translateAnimation.setDuration(250);
                    translateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());

                    //渐变动画
                    AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
                    alphaAnimation.setFillAfter(true);
                    alphaAnimation.setDuration(250);

                    AnimationSet animationSet = new AnimationSet(true);
                    animationSet.addAnimation(translateAnimation);
                    animationSet.addAnimation(alphaAnimation);

                    //设置顶部视图可见，并加载动画
                    topLine.startAnimation(animationSet);
                    topLine.setVisibility(View.VISIBLE);

                    ((Activity) getContext()).getWindow().setStatusBarColor(Color.argb(30, 255, 255, 255));
                }


                old = firstVisibleItem;
            }
        });*/


        return decorView;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }
}
